import direct.directbase.DirectStart
from direct.task import Task
from direct.actor import Actor
from pandac.PandaModules import *
from direct.interval.MetaInterval import *

from direct.gui.DirectGui import *
from direct.interval.IntervalGlobal import *
from direct.showbase import DirectObject

import math


class RoboCamera(DirectObject.DirectObject):
    '''
    Cameraaaaaaaa
    '''
    def __init__(_):
        _.offset = Point3(0,-28,36)
        _.angle = -45.0
        
        base.camera.setPos(_.offset)
        base.camera.setP(_.angle)
        _.camTask = taskMgr.add(_.move,'CameraMover')

        _.limit = (0.2, 0.2)
        _.zoomlimit = (5,10)
        _.zoomamt = 10
        _.rate = 0.03

        ##_.accept('1',_.zoomIn)
        ##_.accept('2',_.zoomOut)
        
        _.mode = 'top'
        _.accept('`',_.changeMode)
        _.target = None

    def setTarget(_, thing):
        _.target = thing

    def zoomIn(_):
        if base.camera.getY() < _.zoomlimit[0]:
            return        
        #movement = LerpPosInterval(base.camera,0.3,base.camera.getPos()+Vec3(0,10,-10),camera.getPos())
        #movement.start()
        _.offset *= (1.0 - _.rate)
        _.zoomamt += _.rate
        #camera.setY(camera.getY()+1)
        #camera.setZ(camera.getZ()-1)

    def zoomOut(_):
        if base.camera.getY() > _.zoomlimit[1]:
            return
        
        #movement = LerpPosInterval(base.camera,0.3,base.camera.getPos()+Vec3(0,-10,10),camera.getPos())
        #movement.start()
        _.offset *= (1.0 + _.rate)
        _.zoomamt -= _.rate
        #camera.setY(camera.getY()-1)
        #camera.setZ(camera.getZ()+1)


    def move(_, task):
        if _.target:
            if(_.mode == 'chase'):
                base.camera.setHpr(_.target.getH(), _.angle, 0)
                base.camera.setPos(_.target, _.offset)

            elif(_.mode == 'flyby'):
                dist2player = (base.camera.getPos() - _.target.getPos()).length()
                if(dist2player > 40.0):
                    base.camera.setPos(_.target, Point3(0,50.0, 1.0))
                
                base.camera.lookAt(_.target)

            elif(_.mode == 'top'):
                base.camera.setPos(_.target.getPos() + _.offset)
                base.camera.lookAt(_.target)

##        if base.mouseWatcherNode.hasMouse():            
##            maus = base.mouseWatcherNode.getMouse()
##            x = maus.getX()
##            y = maus.getY()  
##            
##            #camh = base.camera.getH()
##            #camp = base.camera.getP()
##        
##            #angleradians = 0
##        
##            if x <= -1.0 + _.limit[0]:
##                angleradians = _.rate * (math.pi / 180.0)
##            
##                ##base.camera.setH(camh-_.rate)
##            if x >= 1.0 - _.limit[1]:
##                angleradians = -_.rate * (math.pi / 180.0)
##                ##base.camera.setH(camh-_.rate)
##            if y <= -1.0 + _.limit[0]:
##                base.camera.setP(camh-_.rate)
##            if y >= 1.0 - _.limit[1]:
##                base.camera.setP(camh+_.rate)
##                
##            base.camera.setPos(_.offset[0]*math.sin(angleradians),
##                                _.offset[1]*math.cos(angleradians),
##                                _.offset[2])
            
        return Task.cont



    def setMode(_, newmode):
        _.mode = newmode
        
##        if(_.mode == 'chase'):
##            base.camera.node().reparentTo(_.target)
##            #base.camera.setHpr(_.target.getH(), _.angle, 0)
##            base.camera.setPos(_.target, _.offset)
##            _.limit = (0.2, 0.2)
##            _.zoomlimit = (20,100)
##            _.rate = 0.3
##        elif(_.mode == 'flyby'):
##            _offset = Point3(0,0,1)
##        elif(_.mode == 'top'):
##            _.offset = Point3(0,0,100)
##            base.camera.setPos(0,0,100)
##            base.camera.setP(-90)
##
##            _.limit = (0.2, 0.2)
##            _.zoomlimit = (50,200)
##            _.rate = 0.3
##            
    
    def changeMode(_):
        modes = ['chase', 'top', 'flyby']
        index = modes.index(_.mode) + 1
        if index >= len(modes):
            _.setMode(modes[0])
        else:
            _.setMode(modes[index])
